package com.mj.list;

/**
 * 不对外公开  不可以实例化
 * @param <E>
 */
public abstract class AbstractList<E> implements List<E> {

    /**
     * 动态数组元素的数量
     */
    protected int size;

        @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean contains(E element) {
        return indexOf(element) !=-1;
    }

    @Override
    public void add(E element) {
        add(size, element);
    }


    /**
     * 抽取越界异常
     *
     * @param index
     */
    protected void outOfBounds(int index) {
        throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size);
    }

    protected void rangeCheck(int index) {
        if (index < 0 || index >= size) {
            outOfBounds(index);
        }
    }

    protected void rangeCheckForAdd(int index) {
        if (index < 0 || index > size) {
            outOfBounds(index);
        }
    }
}
